/**  
* @Title: TypeBookController.java  
* @Package com.ssm.demo.controller  
* @Description: TODO(用一句话描述该文件做什么)  
* @author jing Lei 
* @date 2019年1月2日  
* @version V1.0  
*/

package com.ssm.demo.controller;

import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.StringUtils;
import com.ssm.demo.model.TypeBook;
import com.ssm.demo.model.vo.UserVo;
import com.ssm.demo.service.typebook.ITypeBookService;

/**
 * @ClassName: TypeBookController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author jing Lei
 * @date 2019年1月2日
 * 
 */
@Controller
@RequestMapping("/typeBook")
public class TypeBookController {
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(TypeBookController.class);

	/**
	 * itypeBookService
	 */
	@Resource
	private ITypeBookService itypeBookService;

	/**
	 * 展示全部集合数据
	 * 
	 * @param model
	 * @param session
	 *            当前session
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(Model model, HttpSession session, String queryName, String queryValue) {

		List<TypeBook> list = itypeBookService.query(queryName, queryValue);
		model.addAttribute("list", list);
		model.addAttribute("queryName", queryName);
		model.addAttribute("queryValue", StringUtils.isNullOrEmpty(queryName) ? "" : queryValue);
		return "/typeBook/list";
	}

	/**
	 * 添加跳转方法
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add")
	public String add(Model model) {
		return "/typeBook/add";
	}

	/**
	 * 修改方法
	 * 
	 * @param id
	 *            主键
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}")
	public String edit(@PathVariable("id") Integer id, Model model) {
		model.addAttribute("typeBook", itypeBookService.find(id));
		return "/typeBook/edit";
	}

	/**
	 * 保存方法
	 * 
	 * @param typeBook
	 *            实体对你
	 * @param ra
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/saveOrUpdate")
	public String save(TypeBook typeBook, HttpSession session) {
		try {
			UserVo user = (UserVo) session.getAttribute("user");
			if (typeBook.getId() != null) {
				itypeBookService.update(typeBook, user);
			} else {
				itypeBookService.save(typeBook, user);
			}

		} catch (Exception e) {
			log.error("保存发生未知异常", e);
		}
		return "redirect:/typeBook/list";
	}

	/**
	 * 删除方法，可批量删除
	 * 
	 * @param idGroup
	 *            id数据
	 * @param ra
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public String delete(Integer[] idGroup, HttpSession session) {
		try {
			UserVo userVo = (UserVo) session.getAttribute("user");
			List<Integer> idList = Arrays.asList(idGroup);
			List<TypeBook> modelList = itypeBookService.findByIdIn(idList);
			itypeBookService.deleteInBatch(modelList, userVo);

		} catch (Exception e) {
			log.error("删除发生未知异常", e);
		}
		return "redirect:/typeBook/list";
	}

	/**
	 * 
	 * 前端传过来的值与数据库中数据比较
	 * 
	 * @param typeName
	 *            书籍类型名
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/checkName/{name}")
	@ResponseBody
	public String checkName(@PathVariable("name") String typeName, Model model) {

		List<TypeBook> typeBookList = itypeBookService.findByTypeName(typeName);
		if (typeBookList == null || typeBookList.size() == 0) {
			return "true";
		} else {
			return "false";
		}
	}
}
